﻿using System;
using System.Collections.Generic;
using Moonlite.ExtensionApi.XimlInterfaces;

namespace Moonlite.Platform.Ximl {
    public class StringNodeDataProvider : INodeDataProvider {
        bool INodeDataProvider.ParseXimlNode(ref IXimlNode node, out DescriptorBase descriptor) {
            descriptor = null;
            if (node.Name == "String") {
                if (string.IsNullOrEmpty(node["value"])) {
                    throw new XimlInvalidElementException("The String Ximl element must have attribute value.");
                }
                if (string.IsNullOrEmpty(node["resource"])) {
                    throw new XimlInvalidElementException("The String Ximl element must have attribute resource.");
                }
                node["resource"] = node["resource"].TrimStart(new char[] { '$' });
                if (ResourceManager.ResourceExists(node["resource"], ResourceTypes.ImageType) || ResourceManager.ResourceExists(node["resource"], ResourceTypes.StringType)) {
                    throw new XimlException("The resource at " + node["resource"] + " already exists.");
                }
                XimlTree.Instance.Resources.Add(node["resource"], node["value"]);
            }
            return false;
        }
    }
}
